package com.itheima.user.controller;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/validatecode")
public class ValidateCodeController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/send4Order")
    public Result send4Order(@RequestParam String telephone) {
        // 1. 生成验证码
        String code = String.valueOf((int)((Math.random()*9+1)*1000));

        // 2. 存入Redis（5分钟有效）
        redisTemplate.opsForValue().set(
                telephone + RedisMessageConstant.SENDTYPE_ORDER,
                code,
                5, TimeUnit.MINUTES);

        // 3. 发送短信
        System.out.println("发送验证码：" + code + " 到手机：" + telephone);

        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

   
}


